#!/bin/bash


# Arch Linux Desktop Install (archdi)
# -----------------------------------
# author    : MatMoul
#             https://github.com/MatMoul
#             http://sourceforge.net/u/matmoul
# project   : https://github.com/MatMoul/archdi
#             http://sourceforge.net/projects/archdi/
# license   : GPLv3 (http://opensource.org/licenses/GPL-3.0)



apptitle="Arch Linux Desktop Install (archdi) - Version: 2020.04.10.00.16.02 (GPLv3)"
baseurl=https://raw.githubusercontent.com/MatMoul/archdi-pkg/master
cachedir=~/.cache/archdi





# $1: path
menu(){
  require $1 755
  $cachedir/$1
  if [ $? = 0 ]; then
    menu $1
  fi
}


# $1: path
script(){
  require $1 755
  $cachedir/$1
}


# $1: pacman packages
# $2: aur packages
instpkg(){
  if [ ! "$1" = "" ]; then
    clear
    echo "# pacman -S --needed $1"
    pacman -S --needed $1
    pressanykey
  fi
  if [ ! "$2" = "" ]; then
    if [ -f /usr/bin/trizen ]; then
      clear
      echo "# sudo -u aurbuilder trizen -S --needed $2"
      sudo -u aurbuilder trizen -S --needed $2
      pressanykey
    else
      if [ -f /usr/bin/yay ]; then
        clear
        echo "# sudo -u aurbuilder yay -S --needed $2"
        sudo -u aurbuilder yay -S --needed $2
        pressanykey
      else
        if [ -f /usr/bin/aurman ]; then
          clear
          echo "# sudo -u aurbuilder aurman -S --needed $2"
          sudo -u aurbuilder aurman -S --needed $2
          pressanykey
        else
          if [ -f /usr/bin/yaourt ]; then
            clear
            echo "# sudo -u aurbuilder yaourt -S --needed $2"
            sudo -u aurbuilder yaourt -S --needed $2
            pressanykey
          else
            echo "To install AUR packages, you need to install an aurhelper in updates menu !"
          fi
        fi
      fi
    fi
  fi
}


# $1: string
# $2: [title]
choosepkg(){
  if [ "$2" = "" ]; then
    title=$1
  else
    title=$2
  fi
  options=()
  items=$(pacman -Ssq $1)
  for item in $items; do
    options+=("$item" "" off)
  done
  sel=$(whiptail --backtitle "$apptitle" --title "$title" --checklist "" --cancel-button "Back" 0 0 0 \
    "${options[@]}" \
    3>&1 1>&2 2>&3)
  if [ ! "$?" = "0" ]; then
    return 1
  fi
  for itm in $sel; do
    pkg="$pkg $(echo $itm | sed 's/"//g')"
  done
  instpkg "$pkg" "$aurpkg"
  return 0
}


# $1: string
# $2: [title]
chooseaurpkg(){
  if [ "$2" = "" ]; then
    title=$1
  else
    title=$2
  fi
  options=()
  if [ -f /usr/bin/trizen ]; then
    items=$(trizen -Ssq $1)
  else
    if [ -f /usr/bin/yay ]; then
      items=$(yay -Ssq $1)
    else
      if [ -f /usr/bin/aurman ]; then
        items=$(aurman -Ssq $1)
      else
        if [ -f /usr/bin/yaourt ]; then
          items=$(yaourt -Ssq $1)
        fi
      fi
    fi
  fi
  for item in $items; do
    options+=("$item" "" off)
  done
  sel=$(whiptail --backtitle "$apptitle" --title "$title" --checklist "" --cancel-button "Back" 0 0 0 \
    "${options[@]}" \
    3>&1 1>&2 2>&3)
  if [ ! "$?" = "0" ]; then
    return 1
  fi
  for itm in $sel; do
    aurpkg="$aurpkg $(echo $itm | sed 's/"//g')"
  done
  instpkg "$pkg" "$aurpkg"
  return 0
}


# $1: path
# $2: chmod
require(){
  if [ ! -f "$cachedir/$1" ]; then
    path=$(dirname $1)
    mkdir -p $cachedir/$path 2> /dev/null
    cd $cachedir/$path
    wget $baseurl/$1 2> /dev/null
  fi
  chmod $2 $cachedir/$1
  cd $cachedir
}


# $1 : message
# $2 : args
confirm(){
  whiptail --backtitle "$apptitle" --yesno "$1" $2 0 0
}


pressanykey(){
  read -n1 -p "Press any key to continue."
}


# $1 : filename
editfile(){
  editor=$EDITOR
  if [ "$editor" == "" ]; then
    editor="nano"
  fi
  if [ ! -f "/usr/bin/$editor" ]; then
    editor="nano"
  fi
  echo "$editor $1"
  $editor "$1"
}


# $1 : servicename
# $2 : [displayname]
svcstart(){
  if [ "$2" = "" ]; then
    displayname="$1"
  else
    displayname="$2 ($1)"
  fi
  if (confirm "Start $displayname service ?\n\nsystemctl start $1") then
    clear
    echo "systemctl start $1"
    systemctl start $1
    pressanykey
  else
    exit 1
  fi
}

# $1 : servicename
# $2 : [displayname]
svcrestart(){
  if [ "$2" = "" ]; then
    displayname="$1"
  else
    displayname="$2 ($1)"
  fi
  if (confirm "Restart $displayname service ?\n\nsystemctl restart $1") then
    clear
    echo "systemctl restart $1"
    systemctl restart $1
    pressanykey
  else
    exit 1
  fi
}

# $1 : servicename
# $2 : [displayname]
svcstop(){
  if [ "$2" = "" ]; then
    displayname="$1"
  else
    displayname="$2 ($1)"
  fi
  if (confirm "Stop $displayname service ?\n\nsystemctl stop $1") then
    clear
    echo "systemctl stop $1"
    systemctl stop $1
    pressanykey
  else
    exit 1
  fi
}

# $1 : servicename
# $2 : [displayname]
svcenable(){
  if [ "$2" = "" ]; then
    displayname="$1"
  else
    displayname="$2 ($1)"
  fi
  if (confirm "Start $displayname service at boot ?\n\nsystemctl enable $1") then
    clear
    echo "systemctl enable $1"
    systemctl enable $1
    pressanykey
  #else
  #  exit 1
  fi
}

# $1 : servicename
# $2 : [displayname]
svcdisable(){
  if [ "$2" = "" ]; then
    displayname="$1"
  else
    displayname="$2 ($1)"
  fi
  if (confirm "Disable $displayname service at boot ?\n\nsystemctl disable $1") then
    clear
    echo "systemctl disable $1"
    systemctl disable $1
    pressanykey
  #else
  #  exit 1
  fi
}



while (( "$#" )); do
  case $1 in
    --root)
      menu menu
      exit 0
    ;;
    --chroot)
      require menu 755
      sed -i "/options+=(\"Shutdown\" \"\")/d" menu
      menu menu
      exit 0
    ;;
  esac
  shift
done
